package net.zjitc.service.impl;

import net.zjitc.mapper.AlbumMapper;
import net.zjitc.mapper.GoodsMapper;
import net.zjitc.pojo.Album;
import net.zjitc.pojo.Goods;
import net.zjitc.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private AlbumMapper albumMapper;

    @Override
    @Cacheable(value = "goodsCache", key = "'goodlist'")
    public List<Goods> findAll() {
        List<Goods> goodsList = goodsMapper.findAll();
        for (int i = 0; i < goodsList.size(); i++) {
            List<Album> albumList = albumMapper.findById(goodsList.get(i).getId());
            if (!albumList.isEmpty()) {
                goodsList.get(i).setAlbumList(albumList);
            }
        }
        return goodsList;
    }

    @Override
    @Cacheable(value = "goodsCache", key = "#id", unless = "#result == null")
    public List<Goods> findById(Integer id) {
        List<Goods> goodsList = goodsMapper.findById(id);
        return goodsList;
    }

    @Override
    @Caching(
            put = @CachePut(value = "goodsCache", key = "'goodsPage:' + #page + ':' + #pagesize", unless = "#result == null"), // 使用返回对象的ID
            evict = @CacheEvict(value = "goodsCache", key = "'goodlist'") // 清除列表缓存
    )
    public List<Goods> findOnePage(int pagesize, int page) {
        Map<String, Object> params = new HashMap<>();
        params.put("pagesize", pagesize);
        params.put("offset", (page - 1)*pagesize);
        System.out.println("Pagesize: " + pagesize);
        System.out.println("Page: " + page);
        List<Goods> goodsList = goodsMapper.findOnePage(params);
        System.out.println(goodsList);
        for (int i = 0; i < goodsList.size(); i++) {
            List<Album> albumList = albumMapper.findById(goodsList.get(i).getId());
            if (!albumList.isEmpty()) {
                goodsList.get(i).setAlbumList(albumList);
            }
        }
        return goodsList;
    }

    @Override
    @Cacheable(value = "goodsCache", key = "#id", unless = "#result == null")
    public Goods findOne(Integer id) {
        Goods goods = goodsMapper.findOne(id);
        List<Album> albumList = albumMapper.findById(goods.getId());
        if (!albumList.isEmpty()) {
            goods.setAlbumList(albumList);
        }
        System.out.println(goods);
        return goods;
    }

    @Override
    @Caching(
            put = @CachePut(value = "goodsCache", key = "#goods.id", unless = "#result == null"), // 使用返回对象的ID
            evict = @CacheEvict(value = "goodsCache", key = "'goodlist'") // 清除列表缓存
    )
    public void update(Goods goods) {
        goodsMapper.update(goods);
    }

    @Override
    @Caching(evict = {
            @CacheEvict(value = "goodsCache", key = "#id"),
            @CacheEvict(value = "goodsCache", key = "'goodsPage:*'"),
            @CacheEvict(value = "goodsCache", key = "'goodlist'")
    })
    public void deleteAlbum(Integer id) {
        albumMapper.delete(id);
    }

    @Override
    @Caching(
            put = @CachePut(value = "goodsCache", key = "#goods.id",unless = "#result == null"),
            evict = {
                    @CacheEvict(value = "goodsCache", key = "'goodsPage:*'"),
                    @CacheEvict(value = "goodsCache", key = "'goodlist'")
            }// 使用返回对象的ID
    )
    public void addAlbum(Album album) {
        albumMapper.add(album);
    }

    @Override
    @Caching(
            put = @CachePut(value = "goodsCache", key = "#goods.id", unless = "#result == null"), // 使用返回对象的ID
            evict = {
                    @CacheEvict(value = "goodsCache", key = "'goodsPage:*'"),
                    @CacheEvict(value = "goodsCache", key = "'goodlist'")
            } // 清除列表缓存
    )
    public void add(Goods goods) {
        goodsMapper.add(goods);
    }

    @Override
    @Caching(evict = {
            @CacheEvict(value = "goodsCache", key = "#id"),
            @CacheEvict(value = "goodsCache", key = "'goodsPage:*'"),
            @CacheEvict(value = "goodsCache", key = "'goodlist'") // 清除列表缓存
    })
    public void delete(Integer id) {
        goodsMapper.delete(id);
    }

}
